package dk.munthe.stimulus;

import java.util.Random;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class StimulusActivity extends ListActivity {
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  
	  Words words = new Words();
	  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, words.stimuli()));
	  
	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);

	  lv.setOnItemClickListener(new OnItemClickListener() {
	    public void onItemClick(AdapterView<?> parent, View view,
	        int position, long id) {
	      // When clicked, restart activity
	    	Intent intent = getIntent();
	    	finish();
	    	startActivity(intent);
	    }
	  });
	}

	private class Words {
		private String words[];
		private String stimuli[];
		private int num = 5;
		public Words() {
			words = getResources().getStringArray(R.array.words_array);
			generate();
		}
		public void generate() {
			Random random = new Random();
			int index = random.nextInt(words.length);
			int increment = random.nextInt(words.length/num); 
			stimuli = new String[num];
			for (int i = 0; i < num; i++) {
				int j = index+i*increment;
				if (j < words.length) {
					stimuli[i] = words[j];
				} else {
					stimuli[i] = words[j-words.length];
				}
			}
			return;
		}
		public String[] stimuli() {
			return stimuli; 
		}
	}
}